package com.wumao.controller.admin;

import com.wumao.pojo.User;
import com.wumao.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;

/**
 * @author Gaowumao
 * @Decripted
 * @Email none-j@qq.com
 * @create 2021-04-15 11:08
 */
@Controller
@RequestMapping("/admin")
public class LoginController {

    @Autowired
   private UserService userService;

    /*跳转到登录页面*/
    @RequestMapping
    public String loginPage(){
        return "admin/login";
    }

    /*登录操作*/
    @RequestMapping("login")
    public String login(String username, String password,
                        HttpSession session,
                        RedirectAttributes attributes){

        User user = userService.checkUser(username, password);
        if (user != null){
            /*不要将密码传到前端页面*/
            user.setPassword(null);
            session.setAttribute("user",user);
            return "admin/index";
        }else{
            attributes.addFlashAttribute("message","用户名或密码错误");
            return "redirect:/admin";
        }

    }

    /*注销操作*/
    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.removeAttribute("user");
        return "redirect:/admin";
    }
}
